package de.gwtprime.client.widgets;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.places.QuestionPlace;
import de.gwtprime.shared.requestFactory.QuestionProxy;

/**
 * Shows a preview of the question -> one line, just containing title, rating
 * If it is clicked, a detailed view of the question is shown -> QuestionWidget
 * @author jan
 *
 */
public class QuestionPreviewWidget extends Composite {

	interface QuestionPreviewWidgetUiBinder extends UiBinder<Widget, QuestionPreviewWidget>{};
	QuestionPreviewWidgetUiBinder uiBinder = GWT.create(QuestionPreviewWidgetUiBinder.class);
	
	@UiField Anchor title;
	@UiField Label userName;
	@UiField Label voteValue;
	
	public QuestionPreviewWidget(final QuestionProxy question, final ClientFactory clientFactory) {
		this(question, clientFactory, true);
	}
	public QuestionPreviewWidget(final QuestionProxy question, final ClientFactory clientFactory, boolean showAuthor) {
		initWidget(uiBinder.createAndBindUi(this));
		title.setText(question.getTitle());
		
		if(!showAuthor)
			addStyleName("questionHideAuthor");
		
		// Only set for search engines -> prevented in the onclick with preventDefault
		title.setHref("#!Question:"+question.getId());
		
		title.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				event.preventDefault();
				clientFactory.getPlaceController().goTo(new QuestionPlace(question.getId().toString()));
			}
		});
		
		voteValue.setText(String.valueOf(question.getVoteValue()));
		
		
		
		userName.setText(question.getUser()==null ? "-" : question.getUser().getName());
		
		
	}
	
	
}
